package com.fish.yssb.app;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.fish.yssb.Consts;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.db.bean.UserInfoTable;
import com.fish.yssb.db.excel.ReadExcel;
import com.fish.yssb.ui.page.util.PrefeneceUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hy.app101.P101Service;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.realm.Realm;


public class BaseApp extends Application {


    public static BaseApp mInstance;

    public static LoginUser user;


    private String initData = "5";


    public static Activity activityRef;





    @Override
    public void onCreate() {
//        UMConfigure.init(this, "5cfdeb894ca357bc94000522", "5cfdeb894ca357bc94000522", UMConfigure.DEVICE_TYPE_PHONE, null);
//        UMConfigure.setLogEnabled(true);
        super.onCreate();
        mInstance = this;
        init();
        user = new LoginUser();
        UnCeHandler catchExcep = new UnCeHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(catchExcep);

        RealmDb.getInstance().initRealm(this);
        PrefeneceUtil.getInstance().initPrefenece(this);
        initData();
        getUser();

        if(Consts.isRealSQ) {
            P101Service.newInstance().Start101Service();
        }



    }

    private void initData()
    {
       String tmp =  PrefeneceUtil.getInstance().getData(Consts.Prefence_initData,"",false);


//        ReadExcel.getData(getApplicationContext());
        //判断是不是已经添加过初始值
        if(!tmp.equalsIgnoreCase(initData))
        {
//            addInitData();
            RealmDb.getInstance().deleteAllRealmObjects();
            initUserManager();
            ReadExcel.getData(getApplicationContext());
            PrefeneceUtil.getInstance().setData(Consts.Prefence_initData,initData,false);
        }
    }

    private void initUserManager()
    {
        UserInfoTable info = new UserInfoTable();

        info.setName("admin");
        info.setPwd("1234qwer");
        info.setType("管理员");


        ArrayList<UserInfoTable> list = new ArrayList<>();
        list.add(info);
        RealmDb.getInstance().insertRealmObjects(list);
        List<UserInfoTable> arrayList = (List<UserInfoTable>) RealmDb.getInstance().queryRealmObjects(UserInfoTable.class);


    }
    private void addInitData()
    {
        Log.e("=========","count:start");


        ArrayList<BaseInfoTable> list = new ArrayList<>();
        RealmDb.getInstance().deleteAllRealmObjects();


        RealmDb.getInstance().insertRealmObjects(list);
        List<BaseInfoTable> arrayList = (List<BaseInfoTable>) RealmDb.getInstance().queryRealmObjects(BaseInfoTable.class);

        Log.e("=========","count:"+arrayList.size());
        PrefeneceUtil.getInstance().setData(Consts.Prefence_initData,initData,false);
    }

    /*
     * 初始化参数
     */
    private void init() {

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
//                MyActivityManager.getInstance().setCurrentActivity(activity);
                activityRef =  activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });


    }


    public static BaseApp getInstance() {
        return mInstance;
    }

    public static void saveUser()
    {
        Gson gson = new Gson();
        String json = gson.toJson(user).toString();
//        PrefeneceUtil.getInstance().setData(Consts.Prefence_User_info,json,false);
        jsonUser = json;

    }

   public static String jsonUser = "";
    public static void getUser()
    {

//        String json =  PrefeneceUtil.getInstance().getData(Consts.Prefence_User_info,"",false);
        String json = jsonUser;
        Gson gson = new Gson();
        Type type = new TypeToken<LoginUser>() {
        }.getType();
        LoginUser tmp = gson.fromJson(json, type);
        if(tmp !=null)
        {
            user = tmp;
        }else{
            user = new LoginUser();
        }
//        Toast.makeText(PerfeneceActivity.this,arrayList.size()+"",Toast.LENGTH_SHORT).show();
    }


    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }


}
